package com.lifeverse.controller;

import com.lifeverse.common.ApiResponse;
import com.lifeverse.service.PerformanceBenchmarkService;
import io.micrometer.core.annotation.Timed;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * 性能基准测试控制器
 * 提供性能基准测试的REST API接口
 */
@RestController
@RequestMapping("/api/performance/benchmark")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "性能基准测试", description = "性能基准测试相关接口")
public class PerformanceBenchmarkController {
    
    private final PerformanceBenchmarkService benchmarkService;
    
    /**
     * 执行CPU基准测试
     */
    @PostMapping("/cpu")
    @Operation(summary = "执行CPU基准测试", description = "执行CPU密集型计算基准测试")
    @PreAuthorize("hasRole('ADMIN')")
    @Timed(value = "api.benchmark.cpu", description = "CPU benchmark API")
    public ResponseEntity<ApiResponse<PerformanceBenchmarkService.BenchmarkResult>> runCpuBenchmark() {
        try {
            log.info("开始执行CPU基准测试");
            PerformanceBenchmarkService.BenchmarkResult result = benchmarkService.runCpuBenchmark();
            log.info("CPU基准测试完成");
            return ResponseEntity.ok(ApiResponse.success(result));
        } catch (Exception e) {
            log.error("CPU基准测试失败", e);
            return ResponseEntity.ok(ApiResponse.error("CPU基准测试失败: " + e.getMessage()));
        }
    }
    
    /**
     * 执行内存基准测试
     */
    @PostMapping("/memory")
    @Operation(summary = "执行内存基准测试", description = "执行内存分配和操作基准测试")
    @PreAuthorize("hasRole('ADMIN')")
    @Timed(value = "api.benchmark.memory", description = "Memory benchmark API")
    public ResponseEntity<ApiResponse<PerformanceBenchmarkService.BenchmarkResult>> runMemoryBenchmark() {
        try {
            log.info("开始执行内存基准测试");
            PerformanceBenchmarkService.BenchmarkResult result = benchmarkService.runMemoryBenchmark();
            log.info("内存基准测试完成");
            return ResponseEntity.ok(ApiResponse.success(result));
        } catch (Exception e) {
            log.error("内存基准测试失败", e);
            return ResponseEntity.ok(ApiResponse.error("内存基准测试失败: " + e.getMessage()));
        }
    }
    
    /**
     * 执行数据库基准测试
     */
    @PostMapping("/database")
    @Operation(summary = "执行数据库基准测试", description = "执行数据库操作基准测试")
    @PreAuthorize("hasRole('ADMIN')")
    @Timed(value = "api.benchmark.database", description = "Database benchmark API")
    public ResponseEntity<ApiResponse<PerformanceBenchmarkService.BenchmarkResult>> runDatabaseBenchmark() {
        try {
            log.info("开始执行数据库基准测试");
            PerformanceBenchmarkService.BenchmarkResult result = benchmarkService.runDatabaseBenchmark();
            log.info("数据库基准测试完成");
            return ResponseEntity.ok(ApiResponse.success(result));
        } catch (Exception e) {
            log.error("数据库基准测试失败", e);
            return ResponseEntity.ok(ApiResponse.error("数据库基准测试失败: " + e.getMessage()));
        }
    }
    
    /**
     * 执行缓存基准测试
     */
    @PostMapping("/cache")
    @Operation(summary = "执行缓存基准测试", description = "执行缓存操作基准测试")
    @PreAuthorize("hasRole('ADMIN')")
    @Timed(value = "api.benchmark.cache", description = "Cache benchmark API")
    public ResponseEntity<ApiResponse<PerformanceBenchmarkService.BenchmarkResult>> runCacheBenchmark() {
        try {
            log.info("开始执行缓存基准测试");
            PerformanceBenchmarkService.BenchmarkResult result = benchmarkService.runCacheBenchmark();
            log.info("缓存基准测试完成");
            return ResponseEntity.ok(ApiResponse.success(result));
        } catch (Exception e) {
            log.error("缓存基准测试失败", e);
            return ResponseEntity.ok(ApiResponse.error("缓存基准测试失败: " + e.getMessage()));
        }
    }
    
    /**
     * 执行网络基准测试
     */
    @PostMapping("/network")
    @Operation(summary = "执行网络基准测试", description = "执行网络操作基准测试")
    @PreAuthorize("hasRole('ADMIN')")
    @Timed(value = "api.benchmark.network", description = "Network benchmark API")
    public ResponseEntity<ApiResponse<PerformanceBenchmarkService.BenchmarkResult>> runNetworkBenchmark() {
        try {
            log.info("开始执行网络基准测试");
            PerformanceBenchmarkService.BenchmarkResult result = benchmarkService.runNetworkBenchmark();
            log.info("网络基准测试完成");
            return ResponseEntity.ok(ApiResponse.success(result));
        } catch (Exception e) {
            log.error("网络基准测试失败", e);
            return ResponseEntity.ok(ApiResponse.error("网络基准测试失败: " + e.getMessage()));
        }
    }
    
    /**
     * 执行完整基准测试套件
     */
    @PostMapping("/full-suite")
    @Operation(summary = "执行完整基准测试套件", description = "异步执行所有基准测试")
    @PreAuthorize("hasRole('ADMIN')")
    @Timed(value = "api.benchmark.full_suite", description = "Full benchmark suite API")
    public ResponseEntity<ApiResponse<String>> runFullBenchmarkSuite() {
        try {
            log.info("开始执行完整基准测试套件");
            CompletableFuture<Map<String, PerformanceBenchmarkService.BenchmarkResult>> future = 
                    benchmarkService.runFullBenchmarkSuite();
            
            // 异步执行，立即返回
            return ResponseEntity.ok(ApiResponse.success("完整基准测试套件已开始执行，请查看日志获取结果"));
        } catch (Exception e) {
            log.error("启动完整基准测试套件失败", e);
            return ResponseEntity.ok(ApiResponse.error("启动完整基准测试套件失败: " + e.getMessage()));
        }
    }
}